package com.shanglida.android;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.shanglida.R;
import com.shanglida.base.BaseLeadActivity;
import com.shanglida.main.MainActivity;
import com.shanglida.utils.NetWork;

/**
 * @author liu
 * @deprecated 引导页
 * @version 1.0
 * @date 2014-02-25 修改
 * @copyright 尚礼达信息科技有限公司
 */
public class LeadActivity extends BaseLeadActivity {
	// 定义引导界面控件
	private ImageView mregister, mlogin;

	private RelativeLayout Lews;
	// 要展示的一组图片资源
	private Drawable[] pictures;
	// 每张展示图片要执行的一组动画效果
	private Animation[] animations;
	// 当前执行的是第几张图片（资源索引）
	private int currentItem = 0;

	// 退出时间
	// private long mexitTime = 0;

	/*
	 * initView 初始化 initData 监听事件 mLocClient获取当前地理位置
	 * 
	 * @author liu
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lead_actictiy);
		initView();
		initData();
		mLocClient = ((Location) getApplication()).mLocationClient;
		setLocationOption();
		mLocClient.start();
		startNetWork();

	}

	/**
	 * startNetWork 启动监听网络 startService Ioo 注意 这行设置
	 */
	protected void startNetWork() {
		Intent service = new Intent(LeadActivity.this, NetWork.class);
		startService(service);
	}

	/**
	 * 注册监听器
	 */
	protected void initData() {
		mregister.setOnClickListener(this);
		mlogin.setOnClickListener(this);

		// 给每个动画效果设置播放时间
		animations[0].setDuration(1500);
		animations[1].setDuration(3000);
		animations[2].setDuration(1500);
		// 给每个动画效果设置监听事件
		animations[0].setAnimationListener(new GuideAnimationListener(0));
		animations[1].setAnimationListener(new GuideAnimationListener(1));
		animations[2].setAnimationListener(new GuideAnimationListener(2));
		// 设置图片动画初始化默认值为0
		Lews.setBackgroundDrawable(pictures[currentItem]);
		Lews.startAnimation(animations[0]);
	}

	/**
	 * 实现了动画监听接口，重写里面的方法
	 */
	class GuideAnimationListener implements AnimationListener {
		private int index;

		public GuideAnimationListener(int index) {
			this.index = index;
		}

		@Override
		public void onAnimationStart(Animation animation) {
		}

		// 重写动画结束时的监听事件，实现了动画循环播放的效果
		@Override
		public void onAnimationEnd(Animation animation) {
			if (index < (animations.length - 1)) {
				Lews.startAnimation(animations[index + 1]);
			} else {
				currentItem++;
				if (currentItem > (pictures.length - 1)) {
					currentItem = 0;
				}
				Lews.setBackgroundDrawable(pictures[currentItem]);
				Lews.startAnimation(animations[0]);
			}
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
		}
	}

	// 实例化
	protected void initView() {
		Lews = (RelativeLayout) findViewById(R.id.news);
		mregister = (ImageView) findViewById(R.id.lv_register);
		mlogin = (ImageView) findViewById(R.id.lv_login);

		// 实例化引导图片数组
		pictures = new Drawable[] {
				getResources().getDrawable(R.drawable.guide_pic1),
				getResources().getDrawable(R.drawable.guide_pic2),
				getResources().getDrawable(R.drawable.guide_pic3) };

		// 实例化动画效果数组
		animations = new Animation[] {
				AnimationUtils.loadAnimation(this, R.anim.guide_fade_in),
				AnimationUtils.loadAnimation(this, R.anim.guide_fade_in_scale),
				AnimationUtils.loadAnimation(this, R.anim.guide_fade_out) };
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 跳转注册界面
		case R.id.lv_register:
			Intent Regis = new Intent(LeadActivity.this, RegisActivity.class);
			startActivity(Regis);
			break;
		// 跳转登陆界面
		case R.id.lv_login:
			Intent Login = new Intent(LeadActivity.this, LoginActivity.class);
			startActivity(Login);
			break;

		default:
			break;
		}
	}

	/*
	 * @see android.app.Activity#onBackPressed() 返回键
	 */
	@Override
	public void onBackPressed() {
		ExitDialog();
	}

	/**
	 * ExitDialog 退出dialog
	 */
	private void ExitDialog() {
		ShowDialog(getString(R.string.msg_action),
				getString(R.string.exit_ture),
				android.R.drawable.ic_dialog_info,
				getString(R.string.exit_ture_press),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						// 默认关闭界面
						defaultFinish();
					}
				}, getString(R.string.exit_false),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
	}

	/*
	 * @onNewIntent 重写Intent传递 直接退出
	 * 
	 * @deprecated 需要再配置文件中生命"singleTask" 单任务模式，杀死自己
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		if ((Intent.FLAG_ACTIVITY_CLEAR_TOP & intent.getFlags()) != 0) {
			finish();
		}

	}

}